import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/Home.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "home",
    },
    {
      path: "/home",
      name: "home",
      meta: {
        title: "首页",
      },
      component: HomeView,
    },
    {
      path: "/cart",
      name: "cart",
      meta: {
        title: "购物车",
      },
      component: () => import("../views/Cart.vue"),
    },
    {
      path: "/mine",
      name: "mmin",
      meta: {
        title: "我的",
      },
      component: () => import("../views/My.vue"),
    },
    {
      path: "/details",
      name: "details",
      meta: {
        title: "详情",
      },
      component: () => import("../views/Details.vue"),
    },
    {
      path: "/login",
      name: "login",
      meta: {
        title: "登录",
      },
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/register",
      name: "register",
      meta: {
        title: "注册",
      },
      component: () => import("../views/register/Register.vue"),
    },
    {
      path: "/password",
      name: "password",
      meta: {
        title: "设置密码",
      },
      component: () => import("../views/register/Password.vue"),
    },
    {
      path: "/messagecode",
      name: "messagecode",
      meta: {
        title: "获取验证码",
      },
      component: () => import("../views/register/MessageCode.vue"),
    },
    {
      path: "/address",
      name: "address",
      meta: {
        title: "地址管理",
      },
      component: () => import("../views/address.vue"),
    },
    {
      path: "/order",
      name: "order",
      meta: {
        title: "地址管理",
      },
      component: () => import("../views/order.vue"),
    },
    {
      path: "/search",
      name: "search",
      meta: {
        title: "搜搜",
      },
      component: () => import("../views/search.vue"),
    },
    {
      path: "/searchList",
      name: "searchList",
      meta: {
        title: "搜索",
      },
      component: () => import("../views/searchList.vue"),
    },
  ],
});
// 路由防跳
router.beforeEach((to, from) => {
  document.title = to.meta.title;
  if (to.fullPath === "/mine" || to.fullPath === "/cart") {
    if (localStorage.getItem("token")) {
      return true;
    } else {
      return { path: "/login" };
    }
  } else {
    return true;
  }
});
export default router;
